<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="net.sf.json.JSONObject,weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
	BaseBean bb= new BaseBean();
	RecordSet rs = new RecordSet();
	RecordSet rs_mx = new RecordSet();
	RecordSet rs_x = new RecordSet();
	Map<String, String> dataMap = new HashMap<String, String>();
	bb.writeLog("====getgualMM====");
	String mmtype = request.getParameter("mmtype");
	String customer = request.getParameter("customer");//客户
	String diameter = request.getParameter("diamert");
	diameter = getDiameter(diameter);
	String product = request.getParameter("product");
	product = getProduct(product);
	String dopant = request.getParameter("dopant");
	dopant = getdp(dopant);
	String qzd = diameter+customer+product+dopant;
	String  flag = "0";
	String message = "";
	String lsh = "0";
	String lsh_zz = "";
	String lsh_zm = "";
	bb.writeLog("qzd=="+qzd);
	if(mmtype.equals("0")){
		//qual
		String sql = "select 1 from uf_qualmm where qzd ='"+qzd+"'";
		boolean ishave = false;
		rs.execute(sql);
		if(rs.next()){
			ishave = true;
		}
		if(ishave){
			String sql_lsh = "select max(lsh) as lsh from uf_qualmm where qzd ='"+qzd+"'";
			rs.execute(sql_lsh);
			String lsh_old="";
			if(rs.next()){
				lsh_old = Util.null2String(rs.getString("lsh"));
			}
			bb.writeLog("lsh_old==="+lsh_old);
			if(Integer.parseInt(lsh_old)==107){
				flag = "1";
				message = "Error!";
			}else{
				lsh = String.valueOf(Integer.parseInt(lsh_old)+1);
				bb.writeLog("lsh==="+lsh);
				String sql_lshzm = "select dyz from uf_qmmzmdz where lsh='"+lsh+"'";
				rs_mx.execute(sql_lshzm);
				if(rs_mx.next()){
					lsh_zm = Util.null2String(rs_mx.getString("dyz"));
					bb.writeLog("lsh_zm==="+lsh_zm);
					lsh_zz = qzd +lsh_zm+"00";
					flag = "0";
					message = "SUCCESS";
				}else{
					flag = "1";
					message = "Error!";
				}
			}
			
		}else{
			lsh = "0";
			lsh_zz = qzd+"Q0"+"00";
			flag = "0";
			message = "SUCCESS";
		}
	}else if(mmtype.equals("1")){
		//engr
		String sql = "select 1 from uf_englsh where qzd ='"+qzd+"'";
		boolean ishave = false;
		rs.execute(sql);
		if(rs.next()){
			ishave = true;
		}
		if(ishave){
			String sql_lsh = "select max(lsh) as lsh from uf_englsh where qzd ='"+qzd+"'";
			rs.execute(sql_lsh);
			String lsh_old="";
			if(rs.next()){
				lsh_old = Util.null2String(rs.getString("lsh"));
			}
			if(Integer.parseInt(lsh_old)==107){
				flag = "1";
				message = "Error!";
			}else{
				lsh = String.valueOf(Integer.parseInt(lsh_old)+1);
				String sql_lshzm = "select dyz from uf_engdzb where lsh='"+lsh+"'";
				rs_mx.execute(sql_lshzm);
				if(rs_mx.next()){
					lsh_zm = Util.null2String(rs_mx.getString("dyz"));
					lsh_zz = qzd +lsh_zm+"00";
					flag = "0";
					message = "SUCCESS";
				}else{
					flag = "1";
					message = "Error!";
				}
			}
			
		}else{
			lsh = "0";
			lsh_zz = qzd+"J0"+"00";
			flag = "0";
			message = "SUCCESS";
		}
	}else if(mmtype.equals("2")){
		bb.writeLog("2222321");
		//spot
		String sql = "select 1 from uf_spotls where qzd ='"+qzd+"'";
		boolean ishave = false;
		rs.execute(sql);
		if(rs.next()){
			ishave = true;
		}
		bb.writeLog("ishave==="+ishave);
		if(ishave){
			String sql_max = "select max(to_number(ls)) as lsh from uf_spotls where qzd ='"+qzd+"'";
			String lsh_old="";
			rs.execute(sql_max);
			if(rs.next()){
				lsh_old = Util.null2String(rs.getString("lsh"));
			}
			bb.writeLog("lsh_old==="+lsh_old);
			if(Integer.parseInt(lsh_old)==99){
				flag = "1";
				message = "Error!";
			}else{
				bb.writeLog("lsh_old==="+lsh_old);
				lsh  = String.valueOf(Integer.parseInt(lsh_old)+1);
				if(lsh.length()==1){
					lsh_zz = "0"+lsh;
				}else{
					lsh_zz=lsh;
				}
				lsh_zz = qzd +lsh_zz+"00";
				message = "success!";
				flag = "0";
				bb.writeLog("lsh_zz==="+lsh_zz);
			}
		}else{
			lsh = "0";
			lsh_zz = qzd+"00"+"00";
			flag = "0";
			message = "SUCCESS";
		}
	}
	dataMap.put("qzd",qzd);
	dataMap.put("lsh_zz",lsh_zz);
	dataMap.put("lsh",lsh);
	dataMap.put("message",message);
	dataMap.put("flag",flag);
	JSONObject json = JSONObject.fromObject(dataMap);
	out.print(json.toString());
%>
<%!
public String getDiameter(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_dmtdzb where diameter='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}
public String getProduct(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_prodzb where producttype='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}

public String getdp(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_dopdzb where dopant='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}
%>

	